package com.web.remark;

import com.bean.Remark;
import com.bean.User;
import com.service.RemarkService;
import com.tools.SensitiveWordInit;
import com.tools.SensitiveWordUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

public class AddRemarkServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Remark remark=new Remark();
        RemarkService service=new RemarkService();

        HttpSession session=req.getSession();

        try {
            BeanUtils.populate(remark,req.getParameterMap());

            //remark.setPubtime(service.findPubTime(remark.getContent()));
            String content=remark.getContent();
            session.setAttribute("orRemark",content);//原始评论


            //敏感词过滤
            SensitiveWordUtils sensitiveWordUtils=new SensitiveWordUtils();
            String recontent=sensitiveWordUtils.replaceSensitiveWord(content,2);
            remark.setRecontent(recontent);
            req.getSession().setAttribute("reRemark",recontent);//过滤后评论

            int articleId = (int)session.getAttribute("articleId");
            remark.setArticleId(articleId);

            service.addremark(remark);
            req.getRequestDispatcher("/DisplayRemark").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
            //System.out.println("评论添加失败！！！");
        }



    }
}
